package com.nd.microgame;

import androidx.appcompat.app.AppCompatActivity;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.nd.microgame.assetmanagement.AssetMgr;
import com.nd.microgame.assetmanagement.CheckUpdateResult;
import com.nd.microgame.web.CustWebView;
import com.nd.microgame.widget.CustProgressBar;
import #_package_name_#.R;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private CustWebView mCustWebView ;

    private RelativeLayout mPreview;

    public static final int RC_REQUEST = 1001;

    public static final int RC_SIGN_IN = 9001;

  private List<CustProgressBar> mCustProgressBars = new ArrayList<>();


  private int[] iProgressBarIds = {
         R.id.prg1, R.id.prg2, R.id.prg3
  };


    public static final int ERROR_CODE_INTERNAL_ERROR = 0;
    public static final int ERROR_CODE_INVALID_REQUEST = 1;
    public static final int ERROR_CODE_NETWORK_ERROR = 2;
    public static final int ERROR_CODE_NO_FILL = 3;

  private boolean isExit = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        //去除标题
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        //去除状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //关闭熄屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        //加载主视图
        setContentView(R.layout.activity_main);

        mPreview = this.findViewById(R.id.preview);

        //获取WebView控件
        mCustWebView = this.findViewById(R.id.web_view);

        for(int i=0;i<iProgressBarIds.length;i++) {
            CustProgressBar bar = this.findViewById(iProgressBarIds[i]);
            mCustProgressBars.add(bar) ;
        }


        initView(); 
        initAsset();

    }

    private void initAsset() {
        //判断 android_asset 下是否有google/res目录，有则直接加载index.html ; 否则下载res，下载成功后再加载index.html
        AssetMgr.ROOT = "google";
        final AssetMgr assetMgr = new AssetMgr(this); 
        assetMgr.checkUpdate("#_content_url_#",new CheckUpdateResult(this,assetMgr)); 
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    /**
     * 双击返回键退出
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (isExit) {
                this.finish();
                System.exit(0);
            } else {
                Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
                isExit = true;
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        isExit= false;
                    }
                }, 2000);
            }
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    public void initView() {
       this.mCustWebView.setVisibility(View.GONE);
       this.mPreview.setVisibility(View.VISIBLE);
       for(int i=0;i<iProgressBarIds.length;i++) {
           mCustProgressBars.get(i).setVisibility(View.GONE);
       }
       this.setProgressBarVisibility(0,View.VISIBLE);
    }

    /**
     * 设置某一个bar为
     * @param index
     * @param v
     */
    public void setProgressBarVisibility(int index,int v) {
        mCustProgressBars.get(index).setVisibility(v);
    }

    public void showProgress(int index,String text,int p) {
        this.setProgressBarVisibility(index,View.VISIBLE);
        if(-1==p) {
            mCustProgressBars.get(index).setProgress(text);
        }else{
            mCustProgressBars.get(index).setProgress(text,p);
        }
    }

    public void showProgress(String text,int p) {
        if(-1==p) {
            mCustProgressBars.get(0).setProgress(text);
        }else{
            mCustProgressBars.get(0).setProgress(text,p);
        }
    }

    public void startGame(String file) {
        this.mPreview.setVisibility(View.GONE);
        this.mCustWebView.setVisibility(View.VISIBLE);
        mCustWebView.loadUrl(file);
    }

    public static class LoginResp implements Serializable {

        private String  name;

        private String  pwd;

        private String type;

        public LoginResp(String  name,String  pwd,String type) {
            this.name = name;
            this.pwd = pwd;
            this.type = type;
       }
    }

}
